package com.example.payer.vm

import androidx.lifecycle.MutableLiveData
import com.example.common.view.vm.BaseViewModel
import com.example.payer.etity.Payer
import com.example.payer.etity.PayerEntity
import com.example.payer.etity.PostPayerEntity
import com.example.payer.model.PayerRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class PayerViewModel @Inject constructor(private val repo:PayerRepository):BaseViewModel() {
    val mPayerSuccess = MutableLiveData<List<Payer>>()
    fun getPayer(id:Int){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.getPayer(id)
            if(code == 200){
                mPayerSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
    val mPayerDetailSuccess = MutableLiveData<Payer>()
    fun getPayerDetail(id:Int){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.getPayerDetail(id)
            if(code == 200){
                mPayerDetailSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
    val mPostPayerSuccess = MutableLiveData<PostPayerEntity>()
    fun postPayer(body:PostPayerEntity){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.postPayer(body)
            if(code == 200){
                mPostPayerSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
    val mPutPayerSuccess = MutableLiveData<Payer>()
    fun putPayer(id:Int,body:Payer){
        mViewModelScope.launch(mDispatcher){
            val (code,data,message) = repo.putPayer(id,body)
            if(code == 200){
                mPutPayerSuccess.postValue(data)
            }else{
                mError.postValue(message)
            }
        }
    }
}